//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.server.impl;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.net.server.ClientIdentifier;

public class OptimisticClientIdentifier extends BasicClientIdentifier {
    private static final Logger logger = Logger.getLogger(OptimisticClientIdentifier.class.getName());
    private ClientIdentifier backupClientIdentifier;
    private static final int MAX_TRY_COUNT = 4;

    public OptimisticClientIdentifier() {
    }

    public ClientHandler findFirstClientById(String id) {
        return this.findFirstClientById(id, 0);
    }

    private ClientHandler findFirstClientById(String id, int callCount) {
        ClientHandler foundClientHandler = null;

        try {
            Iterator iterator = this.findAllClient();

            while(iterator.hasNext()) {
                foundClientHandler = this.checkClientId((ClientHandler)iterator.next(), id);
                if (foundClientHandler != null) {
                    break;
                }
            }
        } catch (ConcurrentModificationException var5) {
            if (callCount < 4) {
                ++callCount;
                foundClientHandler = this.findFirstClientById(id, callCount);
            } else {
                logger.finest("Going for backup..");
                foundClientHandler = this.getBackupClientIdentifier().findFirstClientById(id);
            }
        }

        return foundClientHandler;
    }

    public Iterator findAllClientById(String pattern) {
        return this.findAllClientById(pattern, 0);
    }

    private Iterator findAllClientById(String pattern, int callCount) {
        ArrayList list = new ArrayList();
        Pattern p = Pattern.compile(pattern);
        ClientHandler foundClientHandler = null;

        try {
            Iterator iterator = this.findAllClient();

            while(iterator.hasNext()) {
                foundClientHandler = this.checkClientId((ClientHandler)iterator.next(), p);
                if (foundClientHandler != null) {
                    list.add(foundClientHandler);
                }
            }
        } catch (ConcurrentModificationException var7) {
            if (callCount < 4) {
                list = null;
                ++callCount;
                return this.findAllClientById(pattern, callCount);
            }

            logger.finest("Going for backup..");
            return this.getBackupClientIdentifier().findAllClientById(pattern);
        }

        return list.iterator();
    }

    public ClientHandler findClientByKey(String key) {
        return this.findClientByKey(key, 0);
    }

    private ClientHandler findClientByKey(String key, int callCount) {
        ClientHandler foundClientHandler = null;

        try {
            Iterator iterator = this.findAllClient();

            while(iterator.hasNext()) {
                foundClientHandler = this.checkClientKey((ClientHandler)iterator.next(), key);
                if (foundClientHandler != null) {
                    break;
                }
            }
        } catch (ConcurrentModificationException var5) {
            if (callCount < 4) {
                ++callCount;
                foundClientHandler = this.findClientByKey(key, callCount);
            } else {
                logger.finest("Going for backup..");
                foundClientHandler = this.getBackupClientIdentifier().findClientByKey(key);
            }
        }

        return foundClientHandler;
    }

    public Iterator findAllClientByKey(String pattern) {
        return this.findAllClientByKey(pattern, 0);
    }

    private Iterator findAllClientByKey(String pattern, int callCount) {
        ArrayList list = new ArrayList();
        Pattern p = Pattern.compile(pattern);
        ClientHandler foundClientHandler = null;

        try {
            for(Iterator iterator = this.findAllClient(); iterator.hasNext(); foundClientHandler = null) {
                foundClientHandler = this.checkClientKey((ClientHandler)iterator.next(), p);
                if (foundClientHandler != null) {
                    list.add(foundClientHandler);
                }
            }
        } catch (ConcurrentModificationException var7) {
            if (callCount < 4) {
                list = null;
                ++callCount;
                return this.findAllClientByKey(pattern, callCount);
            }

            logger.finest("Going for backup..");
            return this.getBackupClientIdentifier().findAllClientByKey(pattern);
        }

        return list.iterator();
    }

    private synchronized ClientIdentifier getBackupClientIdentifier() {
        if (this.backupClientIdentifier == null) {
            this.backupClientIdentifier = new SyncClientIdentifier();
            this.backupClientIdentifier.setClientHandlerPool(this.clientHandlerPool);
        }

        return this.backupClientIdentifier;
    }
}
